<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();
		// Your own constructor code
		$this->load->helper('url');	 // used for anchors
		$this->load->helper('form');
	}
	
	public function validate(){
		$this->load->model('login_model');
		$validation_result = $this->login_model->login_user();
		
		if($validation_result){
			
			// put data in session
			$data = array(
						'username' => $this->input->post('username'),
						'is_logged_in' => true
					);
			$this->session->set_userdata($data);
			
			// redirect to members area
			$data['main_content'] = 'membersarea_view';
			$this->load->view('includes/template',$data);
		}
		else{
			// redirect to login page
			$data['main_content'] = 'login_view';
			$this->load->view('includes/template',$data);
		}
	}
	
	public function logout(){
		$data = array(
						'username' => '',
						'is_logged_in' => ''
					);
		$this->session->unset_userdata($data);
		$this->session->sess_destroy();
		
		// redirect to login page
		$data['main_content'] = 'logout_view';
		$this->load->view('includes/template',$data);
		
	}
}